<?php
// +----------------------------------------------------------------------
// | Crejoy [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.Crejoy.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <Crejoy@126.com>
// +----------------------------------------------------------------------
namespace app\admin\service;

use app\admin\model\CategoryTypeModel;
use app\admin\model\CategoryModel;
use tree\Tree;
class CategoryService
{
    
    public static function getFullChilds($id,$type_id){
        $child_arr = [];
        function findChildArr($id,$type_id,$child_arr){
            $child_arr[] = $id;
            $list = CategoryModel::where(['parent_id'=>$id,'type_id'=>$type_id])->column('id');
            if(!empty($list)){
                foreach ($list as $val){
                    $child_arr = findChildArr($val,$type_id,$child_arr);
                }
            }
            return $child_arr;
        }
        if(!is_numeric($type_id)){
            $type_id = CategoryTypeModel::where(['label'=>$id])->order('id asc')->value('id');
        }
        $child_arr = findChildArr(intval($id),$type_id,$child_arr);
        return $child_arr;
    }
    
    public static function getCategoryOptionsByTypeId($type_id,$current_id = null,$pid = null,$allNode = false){
        if(empty($type_id)){
            return false;
        }
        
        $where['status'] = 1;
        $where['type_id'] = $type_id;
        if(is_numeric($pid)){
            $where['parent_id'] = $pid;
        }
        
        $result = CategoryModel::where($where)
        ->order(array("sort" => "ASC"))
        ->select()
        ->toArray();
        // var_dump($result);
        $tree = new Tree();
        $tree->icon = ['│ ', '├─ ', '└─ '];
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        $array=array();
        foreach ($result as $r) {
            $r['selected'] = $r['id'] == $current_id ? 'selected' : '';
            $array[] = $r;
        }
        $str = "<option value='\$id' \$selected>\$spacer \$name</option>";
        $tree->init($array);
        $selectCategorys = $tree->getTree(is_numeric($pid)?$pid:0, $str);
        
        if($allNode){
            $selectCategorys = "<option value='0' >全部</option>".$selectCategorys;
        }
        
        return $selectCategorys;
    }
    
    public static function getCategoryOptions($sign,$current_id = null,$pid = null,$allNode = false){
        if(empty($sign)){
            return false;
        }
        $type_id = CategoryTypeModel::where(['label'=>$sign])->order('id asc')->value('id');
        return CategoryService::getCategoryOptionsByTypeId($type_id,$current_id,$pid,$allNode);
    }
    
    public static function getCategoryArrayByTypeId($type_id = null,$field = null,$pid = null){
        if(empty($type_id)){
            return false;
        }
        
        $where['status'] = 1;
        $where['type_id'] = $type_id;
        if(is_numeric($pid)){
            $where['parent_id'] = $pid;
        }
        
        $result = CategoryModel::where($where)
        ->order(array("sort" => "ASC"))
        ->field($field)
        ->select()
        ->toArray();
        
        $tree = new Tree();
        $tree->icon = ['│ ', '├─ ', '└─ '];
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        foreach ($result as $r) {
            $array[] = $r;
        }
        $tree->init($array);
        $category_array = $tree->getTreeArray(is_numeric($pid)?$pid:0);
        return $category_array;
    }
    
    public static function getCategoryArray($sign = null,$field = null,$pid = null){
        if(empty($sign)){
            return false;
        }
        $type_id = CategoryTypeModel::where(['label'=>$sign])->order('id asc')->value('id');
        return CategoryService::getCategoryArrayByTypeId($type_id,$field,$pid);
    }
}